const app = getApp();
var wxbarcode = require('../../utils/orderDetail.js');
const config = require('../../config.js');
var WxParse = require('../../wxParse/wxParse.js');
var orderBillId = ""
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tuikuanActionFlag: true
  },
  /**
 * 退款
 */
  tuikuan: function (e) {
    console.info("开始退款");
    console.info(e.target.dataset.eid);
    console.info(orderBillId);

    let submitData = {
      data: '{' +
        '"code": "' + config.tenantCode + '",' +
        '"orderId": "' + e.target.dataset.eid.externalNumber2 + '",' +
        '"orderBillId": "' + orderBillId + '",' +
        '"refundApplyReason": "开启退款申请"}'
    };
    wx.request({
      url: config.requestUrl + "payOrder/returnPayOrderMoney",
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: submitData,
      success: function (msg) {
        wx.hideLoading()
        console.info("退款返回结果");
        console.info(msg);
        if (msg.data.code != null && msg.data.code == "100") {
          //app.tip(msg.data.msg, "none");
          wx.navigateTo({ url: "orderList" })
        } else {
          app.tip(msg.data.msg, "none");
          return;
        }
      },
      fail: function (msg) {
        app.tip(msg, "none");
        return;
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    //托运清单，富文本处理
    var article = app.companyInfo.consignAgreement == null ? 
    '<strong style="color:red">请联系管理员设置托运协议</strong>' : app.companyInfo.consignAgreement;
    WxParse.wxParse('article', 'html', article, this, 5);

    //设置物流公司名称
    this.setData({ company: app.companyInfo })

    console.log(options.order);
    let tempdh = "";
    let temporderno = "";
    let orderBillMasterId = "";
    if (undefined != options.order && "" != options.order) {
      let result = JSON.parse(options.order);
      // console.log(result);
      tempdh = result.id;
      temporderno = (result.orderNumber == "null" || result.orderNumber == null) ? "" : result.orderNumber;
      orderBillMasterId = (result.orderBillMasterId == "null" || result.orderBillMasterId == null) ? "" : result.orderBillMasterId;
    } else if (null != options.dh && null != options.orderNumber) {
      tempdh = options.dh;
      temporderno = (options.orderNumber == "null" || options.orderNumber == null) ? "" : options.orderNumber;
      orderBillMasterId = (options.orderBillMasterId == "null" || options.orderBillMasterId == null) ? "" : options.orderBillMasterId;
    }
    console.log(tempdh + "-------" + temporderno);
    orderBillId = orderBillMasterId
    //wxbarcode.qrcode('qrcode', temporderno != null ? temporderno : (tempdh != null ? tempdh : ""), 300, 300);
    wxbarcode.qrcode('qrcode', config.tenantCode + "," + temporderno, 300, 300);
    getInfo(this, tempdh, temporderno, orderBillId);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
});

/**
 * 获取信息
 */
function getInfo(_this, dh, orderNumber, orderBillMasterId) {
  queryInfo(_this, dh, orderNumber, orderBillMasterId, function (msg) {
    console.info("已转正订单详情");
    console.log(msg);
    let result = msg.data.content;
    if (undefined != result && typeof result == "object") {
      let real = {};
      for (let t in result) {
        if (result[t] != null && result[t] != "null" && result[t] != "undefined") {
          real[t] = result[t];
        }
      }
      
      console.log(real); 
      real.senderAddress = real.senderAddress != undefined ? real.senderAddress.replace('⊙', '') : "";

      var transportCostIsPay = real.transportCostIsPay
      var transportCost = real.transportCost
      var transportCostRefundStatus = real.transportCostRefundStatus

      var priceDifferencesIsPay = real.priceDifferencesIsPay
      var priceDifferences = real.priceDifferences
      var priceDifferencesRefundStatus = real.priceDifferencesRefundStatus
      var currentStatus = real.currentStatus
      if (currentStatus != null && currentStatus == "发货库存"){
        if (priceDifferencesRefundStatus == "无" && transportCostRefundStatus == "无") {
          if (priceDifferencesIsPay == "YES" || transportCostIsPay == 'YES') {
            _this.setData({
              tuikuanActionFlag: false
            })
          } else {
            _this.setData({
              tuikuanActionFlag: true
            })
          }
        } else if (priceDifferencesRefundStatus == "无" && transportCostRefundStatus != "无") {
          if (priceDifferencesIsPay == "YES") {
            _this.setData({
              tuikuanActionFlag: false
            })
          } else {
            _this.setData({
              tuikuanActionFlag: true
            })
          }
        } else if (priceDifferencesRefundStatus != "无" && transportCostRefundStatus == "无") {
          if (transportCostIsPay == 'YES') {
            _this.setData({
              tuikuanActionFlag: false
            })
          } else {
            _this.setData({
              tuikuanActionFlag: true
            })
          }
        }
      } 
      
      _this.setData({ res: real });
    } else {
      _this.setData({
        showView: false,
        message: "抱歉，没有找到相关数据！",
        imagePath: "../images/noData.png"
      });
    }
  })
}

/**
 * 请求数据
 */
function queryInfo(_this, dh, orderNumber, orderBillMasterId,callback) {
  wx.showLoading({ mask: true, title: "加载中" });
  wx.request({
    url: config.requestUrl + "order/getOrderInfo",
    method: "GET",
    header: {
      "Content-Type": "application/x-www-form-urlencoded"//;charset=utf-8
    },
    data: { "id": "", "code": config.tenantCode, "orderNo": orderNumber, "orderBillMasterId": orderBillMasterId},
    success: function (msg) {
      callback(msg);
      wx.hideLoading();
    },
    fail: function (msg) {
      // console.log(msg);
      _this.setData({
        showView: false,
        message: "抱歉，没有找到相关数据！",
        imagePath: "../images/noData.png"
      });
      wx.hideLoading();
    }
  });
}